package com.piece.mall.service;

import com.piece.core.framework.util.object.ObjectUtil;
import com.piece.core.jpa.repository.BaseService;
import com.piece.file.client.FileClientFactory;
import com.piece.file.model.TbSysFile;
import com.piece.file.service.TbSysFileService;
import com.piece.mall.model.PmsSpuImages;
import com.piece.mall.repository.PmsSpuImagesRepository;
import com.piece.mall.vo.SpuInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional
public class PmsSpuImagesService extends BaseService<PmsSpuImages, String> {

    @Autowired
    private PmsSpuImagesRepository pmsSpuImagesRepository;

    @Resource
    private TbSysFileService tbSysFileService;

    @Autowired
    private FileClientFactory fileClientFactory;

    @PostConstruct
    public void initRepository() {
        setRepository(pmsSpuImagesRepository);
    }

    /**
     * 保存spu图片列表
     */
    public void saveSpuImages(SpuInfoVo spuVo) {
        List<String> images = spuVo.getSpuImages();
        if (ObjectUtil.isNotEmpty(images)) {
            AtomicLong index = new AtomicLong(0);
            List<PmsSpuImages> spuImages = images.stream().map(fileId -> {
                PmsSpuImages image = new PmsSpuImages();
                image.setSpuId(spuVo.getId());
                image.setImgUrl(fileId);
                image.setImgSort(index.incrementAndGet());
                return image;
            }).collect(Collectors.toList());
            spuImages = pmsSpuImagesRepository.batchInsert(spuImages);

            // 3.1维护文件表关联数据
            List<TbSysFile> files = new ArrayList<>();
            spuImages.stream().forEach(image -> {
                TbSysFile file = tbSysFileService.findById(image.getImgUrl());
                if (null != file) {
                    file.setTable(PmsSpuImages.class.getSimpleName());
                    file.setBusinessId(image.getId() + "");
                    files.add(file);
                }
            });
            if (ObjectUtil.isNotEmpty(files)) {
                try {
                    tbSysFileService.batchUpdate(files);
                } catch (Exception e) {
                    log.error("更新文件失败", e);
                }
            }
        }
    }

    /**
     * 保存spu图片列表
     */
    public void deleteSpuImages(Long spuId) {
        List<PmsSpuImages> images = pmsSpuImagesRepository.findByCondition("spuId", spuId, null);
        if (CollectionUtils.isNotEmpty(images)) {
            List<String> deleteFiles = images.stream().map(image -> image.getId().toString()).collect(Collectors.toList());
            pmsSpuImagesRepository.deleteByProperties("spuId", spuId);
            try {
                tbSysFileService.deleteByTableAndBusinessList(PmsSpuImages.class.getSimpleName(), deleteFiles);
            } catch (Exception e) {
                log.error("删除文件失败", e);
            }
        }
    }
}
